import { defineStore } from "pinia";
import { ref, computed } from "vue";

// 组合式API写法
let useTodoStore = defineStore("todo", () => {
  let todo = ref([{ id: 1, title: "吃饭" }, { id: 2, title: "睡觉" }]);
  let attr = ref([1, 2, 3, 4]);
  // 计算属性
  let total = computed(()=>{
        return attr.value.reduce((pre:any,next:any)=>{
             return pre+next
        },0)
  })
  // 必须返回一个对象:属性与方法可以提供组件使用
  return {
    todo,
    attr,
    total,
    updateTodo() {
      todo.value.push({ id: 3, title: "学习" });
    }
  };
});

export default useTodoStore;